import os
import json

def count_categories(json_file_path, resource_folder_path):
    """
    统计JSON文件中存在资源的不同类别的总数
    
    参数:
    json_file_path (str): JSON文件的路径
    resource_folder_path (str): 资源文件夹的路径
    
    返回:
    dict: 包含每个类别及其计数的字典
    """
    # 打开并加载JSON文件
    with open(json_file_path, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    # 初始化类别计数器
    category_counts = {}
    
    # 定义has_prefix函数
    def has_prefix(prefix, folder_path):
        for filename in os.listdir(folder_path):
            if filename.startswith(prefix):
                return True
        return False
    
    # 遍历JSON数据
    for item_id, item_data in data.items():
        name = item_data.get('name')
        category = item_data.get('category')
        
        # 检查name是否存在
        if name is None:
            continue
        
        # 检查资源文件是否存在
        if has_prefix(name, resource_folder_path):
            # 更新类别计数
            category_counts[category] = category_counts.get(category, 0) + 1
    
    return category_counts

# 示例用法
if __name__ == "__main__":
    json_path = "not_video_tasks.json"  # 替换为实际的JSON文件路径
    resource_folder = "/Users/liuwei/Downloads/yuanbao_images"  # 替换为实际的资源文件夹路径
    
    result = count_categories(json_path, resource_folder)
    print("类别统计结果:")
    for category, count in result.items():
        print(f"{category}: {count}")
